package cn.ctodb.wxmp.service;

import cn.ctodb.wxmp.domain.Mp;
import cn.ctodb.wxmp.repository.MpRepository;
import cn.ctodb.wxmp.repository.search.MpSearchRepository;
import cn.ctodb.wxmp.service.dto.MpDTO;
import cn.ctodb.wxmp.service.mapper.MpMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.Arrays;

import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;

/**
 * Service Implementation for managing Mp.
 */
@Service
@Transactional
public class MpService {

    private final Logger log = LoggerFactory.getLogger(MpService.class);

    @Inject
    private MpRepository mpRepository;

    @Inject
    private MpMapper mpMapper;

    @Inject
    private MpSearchRepository mpSearchRepository;

    /**
     * Save a mp.
     *
     * @param mpDTO the entity to save
     * @return the persisted entity
     */
    public MpDTO save(MpDTO mpDTO) {
        log.debug("Request to save Mp : {}", mpDTO);
        Mp mp = mpMapper.mpDTOToMp(mpDTO);
        mp = mpRepository.save(mp);
        MpDTO result = mpMapper.mpToMpDTO(mp);
        mpSearchRepository.save(mp);
        return result;
    }

    /**
     *  Get all the mps.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<MpDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Mps");
        Page<Mp> result = mpRepository.findAll(pageable);
        return result.map(mp -> mpMapper.mpToMpDTO(mp));
    }

    /**
     *  Get one mp by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true)
    public MpDTO findOne(Long id) {
        log.debug("Request to get Mp : {}", id);
        Mp mp = mpRepository.findOne(id);
        MpDTO mpDTO = mpMapper.mpToMpDTO(mp);
        return mpDTO;
    }

    /**
     *  Delete the  mp by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Mp : {}", id);
        mpRepository.delete(id);
        mpSearchRepository.delete(id);
    }

    /**
     * Search for the mp corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<MpDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of Mps for query {}", query);
        Page<Mp> result = mpSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(mp -> mpMapper.mpToMpDTO(mp));
    }

    public boolean verifyUrl(String token, String signature, String timestamp, String nonce) {
        String[] array = new String[] { token, timestamp, nonce};
        StringBuffer sb = new StringBuffer();
        // 字符串排序
        Arrays.sort(array);
        for (int i = 0; i < 3; i++) {
            sb.append(array[i]);
        }
        return signature.equals(DigestUtils.sha1Hex(sb.toString()));
    }

}
